package com.skyable.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.skyable.common.constants.exception.CommonExcpConstants;

import java.util.Map;

/**
 * @author GaoYuan
 * @title: ParamUtil
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1410:11
 */
public class ParamUtil {

    /**
     * 检查请求的参数中参数不为空
     * @param map
     * @param params
     * @throws IllegalArgumentException
     */
    public static void checkParams(Map<String, ?> map,
                            String... params) throws IllegalArgumentException {
        for (String param : params) {
            if (param == null) {
                continue;
            }
            if (map == null || map.get(param) == null || map.get(param).toString().length() == 0) {
                /*缺少参数*/
                throw new IllegalArgumentException(CommonExcpConstants.ERROR_MSG_LACK_PARAM + ", [" + param + "] is empty");
            }
        }
    }

    /**
     * 检查请求的参数中参数不为空
     * @param paramObj
     * @param params
     * @throws IllegalArgumentException
     */
    public static void checkParams(JSONObject paramObj,
                            String... params) throws IllegalArgumentException {
        for (String param : params) {
            if (param == null) {
                continue;
            }
            if (paramObj == null || !paramObj.containsKey(param) || paramObj.get(param) == null) {
                /*缺少参数*/
                throw new IllegalArgumentException(CommonExcpConstants.ERROR_MSG_LACK_PARAM + ", [" + param + "] is empty");
            }
        }
    }

    /**
     * 检查请求的参数中参数不为空
     * @param params
     * @throws IllegalArgumentException
     */
    public static void checkParams(Object... params) throws IllegalArgumentException {
        for (Object param : params) {
            if (param == null) {
                /*缺少参数*/
                throw new IllegalArgumentException(CommonExcpConstants.ERROR_MSG_LACK_PARAM);
            }
        }
    }
}
